package com.hmdp.config;

import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import io.jsonwebtoken.security.Keys;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.crypto.SecretKey;
import java.nio.charset.StandardCharsets;
import java.security.Key;
import java.util.Base64;

@Configuration
@ConfigurationProperties(prefix = "jwt")
@Data
public class JwtConfig {
    private StringBuilder secretKey;
    @Bean
    public SecretKey getSecretKey() {
//        return Jwts.SIG.HS256.key().build().
        while (Base64.getEncoder().encodeToString(secretKey.toString().getBytes(StandardCharsets.UTF_8)).getBytes(StandardCharsets.UTF_8).length < 32) {
            secretKey.append(secretKey);
        }
        return Keys.hmacShaKeyFor(Base64.getEncoder().encodeToString(secretKey.toString().getBytes(StandardCharsets.UTF_8)).getBytes(StandardCharsets.UTF_8));
//        return Keys.hmacShaKeyFor(Base64.getEncoder().encodeToString(secretKey.getBytes(StandardCharsets.UTF_8)).getBytes(StandardCharsets.UTF_8));
//        return Keys.hmacShaKeyFor(secretKey.getBytes(StandardCharsets.UTF_8));
    }
}
